package com.ffyc.resale.web;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffyc.resale.model.Account;
import com.ffyc.resale.model.Result;
import com.ffyc.resale.model.User;
import com.ffyc.resale.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("api/user")
@Api(tags = "用户信息界面")
public class UserController {
    @Autowired
    UserService userService;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    @GetMapping("userInfo")
    public Result findUserInfo(@RequestHeader String userToken){
        User user = userService.findUserInfo(userToken);
        return new Result(200,"获取成功",user);
    }

    @PostMapping("updatepassword")
    public Result updatePassword(@RequestBody Account account,@RequestHeader String userToken){
        if(userService.updatePassword(account,userToken)) {
            return new Result(200,"修改成功",null);
        } else {
            return new Result(201,"状态异常",null);
        }
    }
    @PostMapping("updateUserInfo")
    public Result updateUserInfo(@RequestBody User user,@RequestHeader String userToken){
        userService.updateUserInfo(user,userToken);
        return new Result(200,"修改成功",user);
    }
    @PostMapping("getUser")
    public Result getuser(@RequestBody User form){
        Page<User> user = userService.getUser(form);
        return new Result(200,"成功",user);
    }
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody User form){
        userService.updateUser(form);
        return new Result(200,"成功",null);
    }
    @PostMapping("addUser")
    public Result addUser(@RequestBody User form){
        userService.addUser(form);
        return new Result(200,"成功",null);
    }
    @PostMapping("deleteUser")
    public Result deleteUser(@RequestHeader String adminToken,@RequestBody User form){
        userService.deleteUser(form);
        return new Result(200,"成功",null);
    }
    @PostMapping("searchUser")
    public Result searchUser(@RequestHeader String adminToken,@RequestBody User user){
        Page<User> userPage = userService.searchUser(user);
        return new Result(200,"成功",userPage);
    }
    @PostMapping("userLogin")
    public Result userLogin(@RequestBody User user){
        Result login = userService.login(user);
        return login;
    }
    @PostMapping(path = "Uploadimg")
    public Result Uploadimg(@RequestParam("file") MultipartFile commonsMultipartFile, @RequestParam("oldImg") String oldImg) throws IOException, ClientException {


        //为了避免上传的文件重名,重新生成一个文件名
        String newFileName = new Date().getTime()+commonsMultipartFile.getOriginalFilename();
        //连接oss
        OSS ossClient = new OSSClientBuilder().build("https://"+endpoint,  accessKeyId, accessKeySecret);
        //长传文件
        ossClient.putObject(bucketName, newFileName,
                commonsMultipartFile.getInputStream());
        //拼接文件在oss中访问地址
        String url = "https://"+bucketName+"."+endpoint+"/"+newFileName;
        Result result =new Result(200,"成功",url);
        return result;
    }

    @GetMapping(path = "deleteImg")
    public Result deleteImg(@RequestParam("imgurl")String imgurl) throws IOException, ClientException {
        OSS ossClient = new OSSClientBuilder().build("https://"+endpoint,  accessKeyId, accessKeySecret);
        int num= ("https://".length()+bucketName.length()+".".length()+endpoint.length()+"/".length());
        String url=imgurl.substring(num+1);
        String url1="";
        char[] charArray = url.toCharArray();
        for (int i = 0; i < url.length()-1; i++) {
            url1+=charArray[i];
        }
        ossClient.deleteObject(bucketName, url1);
        System.out.println(url);
        Result result =new Result(200,"成功",null);
        ossClient.shutdown();
        return result;
    }

    /*
    判断用户是否点赞帖子
     */
    @RequestMapping(path = "/checkIsDz",method = RequestMethod.GET)
    public Result checkIsDz(Integer forumId,@RequestHeader("userToken") String userToken){
        int res = userService.checkIsDz(forumId,userToken);
        return new Result(200,"发送成功",res);
    }

    @RequestMapping(path = "/dzoper", method = RequestMethod.GET)
    public Result dzoper(Integer forumId,Boolean isDZ,@RequestHeader("userToken") String userToken) {
        userService.dzoper(forumId, isDZ, userToken);
        return new Result(200, "操作成功", null);
    }
}
